Repository navigation
Rendering: sprites take about 45% less CPU time to update and draw - #1916
Merged
Merged
Conversation
When the CPU is the limit, most of a sprite's cost was bookkeeping around it rather than the draw itself. Five changes, none of them public: - Renderable positions (ObservableVector2d/3d, ObservablePoint) no longer go through a Proxy: plain setters fire the same callback. revoke() swaps in a revoked proxy, so a use after revoke still throws. - transformQuadCorners and Bounds.addFrame skip the full matrix apply for an affine matrix, with the same arithmetic in the same order, so the corners are bit-identical. - Color.copy and Matrix3d.copy copy element by element, which makes the renderer's save/restore cheaper. - A renderable with no post effects gets a shared empty chain, and Canvas returns early from beginPostEffect: no array built twice per draw. - Container.update asks the cameras in a plain loop, with no closure per child, and stops at the first camera that sees it. 10,000 sprites, Chromium, per frame (update + draw): WebGL 8.6 -> 4.9 ms, rotating 14.5 -> 6.1 ms, 100 containers 10.0 -> 5.8 ms; WebGPU 9.7 -> 5.4 ms. Frames are byte-identical to master on WebGL, WebGPU and Canvas. New specs: affine-corners-exact, renderable-position-updates, renderer-state-between-sprites, container-camera-visibility, plus cases in renderable, camera-minimap-coords and mat3d. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SeWCkETjW35ZbXDRswV1hx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When the CPU is the bottleneck, most of what a sprite cost each frame was bookkeeping around it, not the draw itself. A profile at 10,000 sprites put most of it in
Renderable.posandanchorPointgoing through aProxy, so every write ranupdateBoundsthrough it. Next came the matrix work for each quad's corners, the renderer's save/restore copies, an effect-chain array built for every renderable, and a closure made for every child inContainer.update.Five changes, none of them public:
ObservableVector2d,ObservableVector3dandObservablePointuse plain setters that fire the same callback.revoke()swaps in a revoked proxy, so a use afterrevoke()still throws. A write topos.xcosts about 2 ns instead of 34.transformQuadCornersandBounds.addFrameskip the fullapplyand compute each corner with the same arithmetic, in the same order. The corners are bit-identical, which a new spec checks againstapply.Color.copyandMatrix3d.copycopy element by element.TypedArray#setcost about 6 ns per call.Matrix3d.copykeepssetfor a source that is not 16 entries long.postEffectsgets one shared, frozen empty chain, and Canvas'sbeginPostEffectreturns early. Before, two arrays were built for every draw.Container.updateasks the stage's cameras in a plain loop, with no closure per child. It stops at the first camera that sees the child; with one camera nothing changes.10,000 sprites, headless Chromium, 600 frames, 4 runs each, ranges:
Canvas drawing is mostly the browser's own work, so only its update gets faster. Frames are byte-identical to master on WebGL, WebGPU and Canvas, for moving, rotating, nested and faded sprites.
One more change was measured and dropped: reading
width,heightand the anchor once inupdateBounds. It saved a few nanoseconds per call, and its frame-time effect came from V8's inlining, not from the work it saved.Type of change
Checklist
pnpm lintpasses)pnpm testpasses)pnpm build)The new specs were written by breaking each change on purpose and checking which mistakes the suite missed:
affine-corners-exact: corners and bounds equalapplyexactly.renderable-position-updates: moves inside an offset container,pos.x +=every frame,set,setPosition,setMutedandanchorPoint, counting bounds updates.renderer-state-between-sprites: a tinted, faded, 3D-turned sprite between two probes, inside a faded group.container-camera-visibility: a main camera and a radar camera, a walker crossing both, a floating HUD.There are also new cases in
renderable.spec.js(a child drops its parent's shader),camera-minimap-coords.spec.js(a minimap clips to its corner) andmat3d.spec.ts(aMatrix2dintoMatrix3d.copy).Full suite on this branch: 543 files, 12,549 tests pass.
Related issues
None.
🤖 Generated with Claude Code
https://claude.ai/code/session_01SeWCkETjW35ZbXDRswV1hx